package cn.dong.statistics.service.impl;

import cn.dong.api.domain.CommonResult;
import cn.dong.statistics.domain.Daily;
import cn.dong.statistics.feign.CourseService;
import cn.dong.statistics.feign.MemberSerivce;
import cn.dong.statistics.mapper.DailyMapper;
import cn.dong.statistics.service.DailyService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 网站统计日数据 服务实现类
 * </p>
 *
 * @author You-Dong
 * @since 2021-03-02
 */
@Service
public class DailyServiceImpl extends ServiceImpl<DailyMapper, Daily> implements DailyService {


    @Autowired
    private CourseService courseService;
    @Autowired
    private MemberSerivce memberSerivce;

    /**
     * 更新数据统计
     *
     * @param day
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateStatisticsInfo(String day) {
        CommonResult<Integer> loginCountByDay = memberSerivce.getLoginCountByDay(day);
        CommonResult<Integer> registerCountByDay = memberSerivce.getRegisterCountByDay(day);
        CommonResult<Integer> courseOnlineByDay = courseService.getCourseOnlineByDay(day);
        // 创建每日信息
        Daily daily = new Daily();
        // 每日课程上线
        daily.setCourseNum(courseOnlineByDay.getData());
        // 每日注册
        daily.setRegisterNum(registerCountByDay.getData());
        // 每日视频播放 TODO
        daily.setVideoViewNum(0);
        // 每日统计日期
        daily.setDateCalculated(day);
        // 每日登录
        daily.setLoginNum(loginCountByDay.getData());
        // 判断插入前是否存在

        // 插入
        super.baseMapper.insert(daily);
    }

    /**
     * 获取统计信息
     *
     * @param begin
     * @param end
     * @return
     */
    @Override
    public Map<String, Map<String, Object>> getInfo(String begin, String end) {
        // 结果
        Map<String, Map<String, Object>> result = new HashMap<>(2);

        Map<String, Object> map = selectByType(begin, end, "register_num");
        Map<String, Object> map1 = selectByType(begin, end, "login_num");
        Map<String, Object> map2 = selectByType(begin, end, "video_view_num");
        Map<String, Object> map3 = selectByType(begin, end, "course_num");

        result.put("register_num", map);
        result.put("login_num", map1);
        result.put("video_view_num", map2);
        result.put("course_num", map3);

        return result;

    }

    /**
     * 根据动态字段，查询不同字段的不同信息
     *
     * @param begin
     * @param end
     * @param type
     * @return
     */
    public Map<String, Object> selectByType(String begin, String end, String type) {
        Map<String, Object> result = new HashMap<>();
        ArrayList<Integer> xData = new ArrayList<>();
        ArrayList<String> yData = new ArrayList<>();
        // 查询各个的统计字段，每个都带有日期
        QueryWrapper<Daily> wrapper = new QueryWrapper<>();
        if (StringUtils.isEmpty(end)) {
            wrapper.ge("date_calculated", begin);
        } else {
            wrapper.between("date_calculated", begin, end);
        }
        wrapper.select("date_calculated", type);
        // 遍历集合
        List<Map<String, Object>> mapList = super.baseMapper.selectMaps(wrapper);
        for (Map<String, Object> map : mapList) {
            Integer count = (Integer) map.get(type);
            String dateCalculated = (String) map.get("date_calculated");
            xData.add(count);
            yData.add(dateCalculated);
        }
        result.put("yData", yData);
        result.put("xData", xData);
        return result;
    }


}
